﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Maps.MapControl;

namespace TrailMap
{
    public class OviTileSource : Microsoft.Maps.MapControl.TileSource, IMapProvider
    {

        private const string hybridURL = @"http://{0}.maptile.maps.svc.ovi.com/maptiler/v2/maptile/newest/hybrid.day/{1}/{2}/{3}/256/png8";
        private const string sateliteURL = @"http://{0}.maptile.maps.svc.ovi.com/maptiler/v2/maptile/newest/satellite.day/{1}/{2}/{3}/256/png8";
        private const string normalURL = @"http://{0}.maptile.maps.svc.ovi.com/maptiler/v2/maptile/newest/normal.day/{1}/{2}/{3}/256/png8";
        public const string terrainURL = "http://{0}.maptile.maps.svc.ovi.com/maptiler/v2/maptile/newest/terrain.day/{1}/{2}/{3}/256/png8";

        private MapType _MapMode = MapType.Normal;

        //Constructor Called by XAML instanciation; Wait for MapMode to be set to initialize services
        public OviTileSource()
            : base()
        {
        }

        public Microsoft.Maps.MapControl.TileSource HybridBase
        {
            get
            {
                OviTileSource retVal = new OviTileSource();
                retVal.MapMode = MapType.Satellite;
                return retVal;
            }
        }

        public string FileExtn
        {
            get
            {
                return "png";
            }
        }

        public MapType MapMode
        {
            get { return _MapMode; }
            set
            {
                _MapMode = value;
            }
        }

        public override Uri GetUri(int x, int y, int zoomLevel)
        {
            try
            {
                string urlTemplate = "";

                switch (this.MapMode)
                {
                    case MapType.Normal:
                        //NormalMap
                        urlTemplate = normalURL;
                        break;
                    case MapType.Hybrid:
                        urlTemplate = hybridURL;
                        break;
                    case MapType.Terrain:
                        urlTemplate = terrainURL;
                        break;
                    default:
                        urlTemplate = sateliteURL;
                        break;
                }

                string url = string.Format(urlTemplate, UrlServerLetters[GetServerNum(x, y, 4)], zoomLevel, x, y);
                return new Uri(url);
            }
            catch (Exception ex)
            {
            }
            return null;
        }
        protected static readonly string UrlServerLetters = "bcde";
        /// <summary>
        /// gets server num based on position
        /// </summary>
        /// <param name="pos"></param>
        /// <returns></returns>
        internal int GetServerNum(int x, int y, int max)
        {
            return (x + 2 * y) % max;
        }
    }
}
